package com.example.windowmanagersample.embedding

import android.app.Activity
import android.app.Dialog
import android.content.ComponentCallbacks
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.util.Log
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.window.layout.WindowMetricsCalculator
import com.example.windowmanagersample.R
import kotlin.jvm.JvmOverloads

class FullDialog @JvmOverloads constructor(context: Context, themeResId: Int = 0) : Dialog(context, themeResId) {
    init {
        val windowCurrentMetrics = WindowMetricsCalculator.getOrCreate()
            .computeCurrentWindowMetrics(context as Activity)
        val width = windowCurrentMetrics.bounds.width()
        val height = windowCurrentMetrics.bounds.height()

        val windowMaxMetrics = WindowMetricsCalculator.getOrCreate()
            .computeMaximumWindowMetrics(context)
        val maxWidth = windowMaxMetrics.bounds.width()
        val maxHeight = windowMaxMetrics.bounds.height()

        Log.d("AAAA", "CURRENT  width: $width, height: $height")
        Log.d("AAAA", "MAX  width: $maxWidth, height: $maxHeight")


        val contentView = LayoutInflater.from(context).inflate(R.layout.window_test_main, null)
        setContentView(contentView)

        val lp: WindowManager.LayoutParams = window!!.attributes
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        } else {
            lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
        }
        window!!.attributes = lp

        val configChangeObserver = object : ComponentCallbacks {
            override fun onConfigurationChanged(newConfig: Configuration) {
                val windowMaxMetrics = WindowMetricsCalculator.getOrCreate()
                    .computeMaximumWindowMetrics(context)
                val maxWidth = windowMaxMetrics.bounds.width()
                val maxHeight = windowMaxMetrics.bounds.height()
            }

            override fun onLowMemory() {
                return
            }
        }
        ownerActivity?.registerComponentCallbacks(configChangeObserver)
    }

    override fun show() {
        super.show()
        val windowCurrentMetrics = WindowMetricsCalculator.getOrCreate()
            .computeCurrentWindowMetrics(ownerActivity!!)
        val width = windowCurrentMetrics.bounds.width()
        val height = windowCurrentMetrics.bounds.height()

        val windowMaxMetrics = WindowMetricsCalculator.getOrCreate()
            .computeMaximumWindowMetrics(ownerActivity!!)
        val maxWidth = windowMaxMetrics.bounds.width()
        val maxHeight = windowMaxMetrics.bounds.height()

        Log.d("AAAA", "CURRENT  width: $width, height: $height")
        Log.d("AAAA", "MAX  width: $maxWidth, height: $maxHeight")

        val lp: WindowManager.LayoutParams = window!!.attributes
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        } else {
            lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
        }
        lp.width = (maxWidth - lp.horizontalMargin * 2).toInt()
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = lp
    }
}